package com.vhall.component.controller.chat;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vhall.component.controller.chat.facade.ChatFacade;
import com.vhall.component.entity.chat.vo.*;
import com.vhall.component.framework.common.annotation.VhallComponent;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.plugin.common.annotation.TokenAuth;
import com.vhall.component.plugin.common.annotation.VssTokenAuth;
import com.vhall.component.plugin.core.service.VssTokenService;
import com.vhall.component.service.chat.ChatService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

/**
 * @author xianglei
 * @date 2021/6/18 15:57
 */

@VhallComponent
public class ChatController implements ChatFacade {

    @Autowired
    private ChatService chatService;

    @Autowired
    private ObjectMapper mapper;

    @Autowired
    private VssTokenService vssTokenService;

    /**
     * 聊天历史信息列表
     *
     * @param reqVo
     * @return
     */
    @VssTokenAuth
    @Override
    public Map lists(ChatListReqVo reqVo) {
        return chatService.lists(reqVo);
    }
    @VssTokenAuth
    @Override
    public Map<String, Object> listMessage(ChatListReqVo reqVo) {
        return chatService.listMessage(reqVo);
    }

    /**
     * 待审核消息列表
     *
     * @param
     * @return
     */
    @VssTokenAuth
    @Override
    public JSONArray auditLists(ChatAuditListReqVo reqVo) {
        Map<String, AuditListRspVo> result = chatService.auditLists(reqVo);

        JSONArray obj = new JSONArray();


        result.keySet().forEach(key -> {
            try {
                Map<String, String> rsp = new HashMap<>(16);
                rsp.put(key, mapper.writeValueAsString(result.get(key)));
                obj.add(rsp);
            } catch (JsonProcessingException e) {
                throw new BusinessException("转化json串出现异常！");
            }
        });


        return obj;
    }

    /**
     * 获取历史消息数据
     *
     * @param
     * @return
     */
    @VssTokenAuth
    @Override
    public JSONObject messageLists(MessageListsReqVo reqVo) {
        return chatService.messageLists(reqVo);
    }

    /**
     * 设置审核开关接口
     *
     * @param
     * @return
     */
    @VssTokenAuth
    @Override
    public JSONObject setChannelSwitch(ChannelSwitchSetReqVo reqVo) {
        return chatService.setChannelSwitch(reqVo);
    }

    /**
     * 获取审核开关状态接口
     *
     * @param
     * @return
     */
    @VssTokenAuth
    @Override
    public JSONObject getChannelSwitch(ChannelSwitchReqVo reqVo) {
        return chatService.getChannelSwitch(reqVo);
    }

    /**
     * 设置是否自动处理聊天数据接口（switch开启能发,不能收,会转到审核频道）
     *
     * @param
     * @return
     */
    @VssTokenAuth
    @Override
    public JSONObject setChannelSwitchOptions(ChannelSwitchOptionReqVo reqVo) {
        return chatService.setChannelSwitchOptions(reqVo);
    }

    /**
     * 审核消息操作
     *
     * @param
     * @return
     */
    @VssTokenAuth
    @Override
    public Boolean applyMessageSend(ApplyMessageSendReqVO reqVO) {
        return chatService.applyMessageSend(reqVO);
    }
    @TokenAuth
    @Override
    public Boolean sendMessage(SendMessageReqVO reqVO) {
        return chatService.sendMessage(reqVO);
    }

    /**
     * 聊天图片上传
     *
     * @param reqVo
     * @return
     */
    @VssTokenAuth
    @Override
    public Map<String, Object> upload(ChatUploadReqVo reqVo) {
        return chatService.upload(reqVo);
    }
}
